const sys = require(`./function/system.js`)
const title = '助学心愿小店签到'
const $ = sys.$(title)

// process.env.ZXXB_XY_COOKIE = 'wdtoken=6d419628;  uid=396895509; login_token=_EwWqqVIQdP7BkKIZopBm2HmFAePLX35QMUeSugb30PTc3ZGYpu_aki08u4pT9YXCKNtn714Ybjz4LCYWwod0_J86maZ6xSjtX-pzmxefEUNrytAzHaM04-PyfFi-c58zym4Ebx9lk_iyRXVnIjpIjbjH6YPkqYmRBp7Ffj9ymTEXSiL-8LI0LKFgqpRWFIvRpsx_jVWtrqvUiTkJJOeKMJlaiQKvdCvjhE0aavdndcdEvottLb-hJvVE8LVg6qjqwYXMVPUU'
let cookies = []
if (process.env.ZXXB_XY_COOKIE) {
    if (process.env.ZXXB_XY_COOKIE.indexOf('&') > -1) {
        cookies = process.env.ZXXB_XY_COOKIE.split('&');
    } else if (process.env.ZXXB_XY_COOKIE.indexOf('\n') > -1) {
        cookies = process.env.ZXXB_XY_COOKIE.split('\n');
    } else {
        cookies = [process.env.ZXXB_XY_COOKIE];
    }
}

sys.run3($, (resList) => {
    return new Promise(async resolve => {
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i]
            let message = ``
            // 签到
            const resp1 = await $.publicJS.sign($, cookie)
            const message1 = getMessage(resp1.status)
            message += `【签到】[序号${i + 1}] [name: ${$.publicJS.getCookieValue(cookie, 'name')}] \n结果：${message1} \n`
            // 签到任务
            const resp2 = await $.publicJS.signTask($, cookie)
            let message2 = getMessage(resp2.status)
            if (message2 === '成功' && !resp2.result) {
                message2 = '您今日已领取签到任务积分'
            }
            message += `【签到任务】[序号${i + 1}] [name: ${$.publicJS.getCookieValue(cookie, 'name')}] \n结果：${message2} \n`
            resList.push(message)
        }
        resolve(resList)
    })
}, (resList) => {

    $.notify.sendNotify(title, resList)
    console.log('resList', resList)
});


function getMessage(status) {
    const code = status.code
    let message
    if (code === 0) {
        message = '成功'
    } else if (code === 2) {
        message = '失败，请检查 cookie 是否有效'
    } else {
        message = status.message
    }
    return message
}
